﻿using System.ServiceModel.Channels;
using System.ServiceModel;
using System;

using NUnit.Framework;
using RenewalProjects.Libraries.Common.Testing.Attributes;
using RenewalProjects.Libraries.Common.Testing.Interfaces;

namespace RenewalProjects.Libraries.Common.Testing.Tests.Fixtures
{
	[TestFixture]
	[ServiceHostMetadataAttribute(Address = "net.pipe://localhost/testFixture")]
	public class UsesServiceHostActionAttributeFixture : IUsesServiceHosts
	{
		#region Test Methods

		[Test]
		[IntegrationTest]
		[ServiceHostMetadataAttribute(Contract = typeof(ITestService), Service = typeof(ServiceA))]
		public void Check_that_fixture_config_works()
		{
			Assert.AreEqual("net.pipe://localhost/testFixture", Address);
		}

		[Test]
		[IntegrationTest]
		[ServiceHostMetadataAttribute(Contract = typeof(ITestService), Service = typeof(ServiceA))]
		public void Check_that_service_host_starts_up_per_default()
		{
			// I use this basic channel to make sure it can call this service
			ChannelFactory<ITestService> channelFactory = new ChannelFactory<ITestService>(Binding, Address);
			ITestService channel = channelFactory.CreateChannel();
			Assert.AreEqual("Value A", channel.GetSomeValue());
		}

		#endregion

		#region Properties

		public ServiceHost Host { get; set; }
		public Binding Binding { get; set; }
		public string Address { get; set; }

		#endregion

		#region Helper Code

		[ServiceContract]
		public interface ITestService
		{
			[OperationContract]
			string GetSomeValue();
		}

		public class ServiceA : ITestService
		{
			public string GetSomeValue()
			{
				return "Value A";
			}
		}

		#endregion
	}
}